var MergePurchase = function () {
  var template = /* html */ `
    <div class="merge-purchase">
      <div style="margin-top:10px">
        <el-input-number is-decimal-zero v-model="purchaseValue" :min="0" :controls="false" size="medium" :precision="precision"
          placeholder="请输入合并结算入库数量" style="width:100%">
        </el-input-number>
      </div>
      <div style="text-align:center;margin-top:10px">
        <el-button size="medium" type="primary" @click="ensure">合并计算</el-button>
      </div>
    </div>
  `;

  return {
    props: {
      warehouses: {
        type: Array,
        required: true
      }
    },

    computed: {
      precision: function () {
        var warehouse = this.warehouses[0];
        var unit = warehouse.inCountUnit;
        return SysUtil.getMaterialNumberPrecision(unit, 4);
      }
    },

    data: function () {
      return {
        purchaseValue: undefined
      };
    },

    methods: {
      ensure: function () {
        var purchaseValue = this.purchaseValue;
        if (SysUtil.isEmpty(purchaseValue)) {
          this.$alert('请输入合并结算入库数量', '信息');
          return;
        }

        if (purchaseValue === 0) {
          this.$alert('合并结算入库数量不能为0', '信息');
          return;
        }

        var warehouseList = this.warehouses;
        var sumCount = _.sumBy(warehouseList, 'sumMoneyCount');
        var precision = this.precision;
        var realSumCount = 0;
        var warehouseCountList = [];
        for (var i = 0; i < warehouseList.length; i++) {
          var warehouse = warehouseList[i];
          var sumMoneyCount = warehouse.sumMoneyCount;
          sumMoneyCount = sumMoneyCount / sumCount * purchaseValue;
          sumMoneyCount = _.floor(sumMoneyCount, precision);
          realSumCount += sumMoneyCount;
          warehouseCountList.push(sumMoneyCount);
        }

        var rangeSumCount = _.round(purchaseValue - realSumCount, precision + 2);
        if (rangeSumCount === 0) {
          this.toMerge(warehouseCountList);
          return;
        }
        var self = this;
        this.$confirm(`合并结算入库数量还有${rangeSumCount}未分配，是优先分配到选择靠前的订单，还是舍去不分配`, '信息', {
          distinguishCancelAndClose: true,
          confirmButtonText: '分配',
          cancelButtonText: '不分配'
        }).then(function () {
          self.toMerge(warehouseCountList, rangeSumCount);
        }).catch(function (action) {
          if (action === 'cancel') {
            self.toMerge(warehouseCountList);
          }
        });
      },

      toMerge: function (warehouseCountList, rangeSumCount) {
        if (rangeSumCount) {
          var precision = this.precision;
          var len = _.round(rangeSumCount * Math.pow(10, precision), 0);
          var addCount = 1/Math.pow(10, precision);
          for (var i = 0; i < len; i++) {
            warehouseCountList[i] += addCount;
          }
        }

        var warehouseList = this.warehouses;
        for (var i = 0; i < warehouseList.length; i++) {
          var warehouse = warehouseList[i];
          var sumMoneyCount = warehouseCountList[i];
          warehouse.sumMoneyCount = sumMoneyCount;

          var inCountPrice = warehouse.inCountPrice;
          var inOutMoney = inCountPrice * sumMoneyCount;
          inOutMoney = _.round(inOutMoney, 2);
          warehouse.inOutMoney = inOutMoney;
        }
        this.$emit('merge-success');
      }
    },

    template: template
  };
}();
